// pages/posts/post-detail.js
var postsData = require('../../../data/posts-data.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //接收一下，跳转过来传递的参数---直接绑定到阿option属性上
    var postId = options.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId]
    //传递个页面作为起始参数
    this.setData({
      postData: postData
    });

    //判断是否收藏
    var postsCollected = wx.getStorageSync('postsCollected');
    if (postsCollected){//如果存在，代表已经收藏过了
      var collected = postsCollected[postId];
      this.setData({
        collected: collected
      })
    }else{
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('postsCollected', postsCollected);
    }
  },
  onColletionTap:function(event){
   this.getPostsCollectedSyc();
   // 弹框提示
    wx.showToast({
      title: this.data.collected? "收藏成功" : "取消成功",
      duration:1000,
      icon:'success'
    });
    //this.showModal('postsCollected', 'postsCollected');
  },

  //分享按钮的事件
  onShareTap:function(event){
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ];
    wx.showActionSheet({
      itemList: itemList,
      itemColor:'#405f80',
      success:function(res){
        // res.cancel 用户是不是点击了取消按钮--
        // res.tapIndex 数组元素的序号，从0开始
        wx.showModal({
          title: "用户 " + itemList[res.tapIndex],
          content: "用户是否取消？" + res.cancel + "暂时无法实现分享功能"
        })
      }
    })
  },
  
  onMusicTap:function(event){
    //获取是哪一个文章对应的音乐
    var currentPostId = this.data.currentPostId;
    var postData = postsData.postList[currentPostId];
    var isPlayingMusic = this.data.isPlayingMusic;
    if(isPlayingMusic){//代表正在播放
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      })
    }else{
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        coverImgUrl: postData.music.coverImg,
      });
      this.setData({
        isPlayingMusic: true
      })
    }
  },

  showModal: function (postsCollectedKey, postsCollected){
    var that = this;
    wx.showModal({
      title: '收藏文章',
      content: '确定收藏次文章吗',
      showCancel:'true',
      cancelText:'取消',
      cancelColor: "#333",
      confirmText: "确认",
      confirmColor: "#405f80",
      success:function(){
        if (res.confirm) {
          wx.setStorageSync(postsCollectedKey, postsCollected);
          // 更新数据绑定变量，从而实现切换图片
          that.setData({
            collected: postCollected
          })
        }
      }
    })
  },

  getPostsCollectedSyc:function(){
    var postsCollected = wx.getStorageSync('postsCollected');
    var collected = postsCollected[this.data.currentPostId];
    //点击之后修改状态，收藏变为收藏，为收藏变收藏
    collected = !collected;
    //将值存到缓存中
    postsCollected[this.data.currentPostId] = collected;
    wx.setStorageSync('postsCollected', postsCollected);

    // 更新数据绑定变量，从而实现切换图片
    this.setData({
      collected: collected
    })
  }

})